package com.pictavore.util

import javax.swing.*
import java.awt.event.*
import java.awt.*

import com.google.inject.*

import groovy.swing.SwingBuilder
import com.pictavore.message.MessageListener
import com.pictavore.message.Messenger
import com.pictavore.message.Message
import com.pictavore.log.LoggerMessage
import com.pictavore.plugin.tool.ToolPlugin

/**
 *
 * @author caleb
 */
class VisualLog implements ToolPlugin {
	JTextArea log = null
    JPanel control = null
    String displayName = "Visual Log"

    @Inject
    Messenger messenger

    void load() {
        def swing = new SwingBuilder()
        log = swing.textArea(font: new Font("Courier", Font.PLAIN, 10))

        control = swing.panel() {
            borderLayout()
            scrollPane(constraints: BorderLayout.CENTER,
                       horizontalScrollBarPolicy: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED,
                       verticalScrollBarPolicy: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED) {
                widget(log)
            }
            button(action: action(name: "Clear Log", closure: {
                log.text = ""
            }), constraints: BorderLayout.SOUTH)
        }

        messenger.addListener(LoggerMessage.ERROR, { Message message ->
            log.append(message.parameters[0])
        } as MessageListener)
        messenger.addListener(LoggerMessage.INFO, { Message message ->
            log.append(message.parameters[0])
        } as MessageListener)
    }

    JComponent getComponent() {
        control
    }
}

